package com.example.administrator.basedemos.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.basedemos.R;
import com.example.administrator.basedemos.activity.ThanExchangeActivity;
import com.example.administrator.basedemos.adapter.RecommendFragmentAdapter;
import com.example.administrator.basedemos.utils.LogUtil;
import com.example.administrator.basedemos.utils.RecycleUtil;
import com.example.administrator.basedemos.view.ClassicsFooter;
import com.example.administrator.basedemos.view.ClassicsHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者     ZCL
 * 创建时间   2018/7/4 11:47
 * 描述	      ${TODO}
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */
public abstract class BaseMarketFragment extends BaseFragment implements BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {

    private static final String TAG = "RecommendFragment";
    @BindView(R.id.recyclerview_mm)
    RecyclerView       mRecyclerviewMm;
    @BindView(R.id.market_rank_l)
    TextView           mMarketRankL;
    @BindView(R.id.market_rank_m)
    TextView           mMarketRankM;
    @BindView(R.id.market_rank_r)
    TextView           mMarketRankR;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout mSmartRefresh;


    private boolean mRankNumLDown;
    private boolean mRankNumMDown;
    private boolean mRankNumRDown;
    private ArrayList<String> mList = new ArrayList<>();
    public  RecommendFragmentAdapter mAdapter;
    private FragmentActivity         mActivity;


    @Override
    public int setLayoutId() {
        return R.layout.fragment_recommend;
    }

    @Override
    public void initData() {
        mActivity = getActivity();
        mRankNumLDown = true;
        mRankNumMDown = true;
        mRankNumRDown = true;
        mList = setData();

        RecycleUtil.setLayoutMangerVertical(mRecyclerviewMm, getActivity());
        mAdapter = new RecommendFragmentAdapter(R.layout.item_recomend, mList);

        //必须添加getparent否则不显示
        setEmptyView(R.layout.empty_view);
        setAboutAdapter();


        //Item子控件的点击事件
        //首先在adapter的convert方法里面通过viewHolder.addOnClickListener绑定一下的控件id
        mAdapter.setOnItemChildClickListener(this);
        mAdapter.setOnItemClickListener(this);
        // mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);


        mSmartRefresh.setRefreshHeader(new ClassicsHeader(getContext()));
        mSmartRefresh.setHeaderHeight(60);

        mSmartRefresh.setRefreshFooter(new ClassicsFooter(getContext()));
        mRecyclerviewMm.setAdapter(mAdapter);
    }

    public abstract void setAboutAdapter();

    public void setEmptyView(int empty_view) {
        if (mAdapter != null && mRecyclerviewMm != null)
            mAdapter.setEmptyView(empty_view, (ViewGroup) mRecyclerviewMm.getParent());
    }

    public abstract ArrayList<String> setData();

    @Override
    public void initListener() {
        super.initListener();

        mSmartRefresh.setOnRefreshListener(refreshLayout -> {
            refreshLayout.finishRefresh(2000, true);
            Toast.makeText(getActivity(), "哈哈哈下拉啊", Toast.LENGTH_SHORT).show();
            mList = refreshData();
            mAdapter.notifyDataSetChanged();
        });
        mSmartRefresh.setOnLoadMoreListener(loadMoreLayout -> {
            //2秒加载成功没有数据
            Toast.makeText(getActivity(), "呵呵呵加载啊", Toast.LENGTH_SHORT).show();
            ArrayList loadMore = loadData(0);
            if (mList != null)
                mList.addAll(loadMore);
            mAdapter.notifyDataSetChanged();
            loadMoreLayout.finishLoadMore(2000, true, true);
        });

    }

    protected abstract ArrayList loadData(int type);

    protected abstract ArrayList refreshData();

    @OnClick({R.id.market_rank_l, R.id.market_rank_m, R.id.market_rank_r})
    public void OnClik(View v) {
        Drawable drawableL = null;
        Drawable drawableM = null;
        Drawable drawableR = null;
        switch (v.getId()) {
            case R.id.market_rank_l:
                setArrowChoice(drawableL, mRankNumLDown, 0);
                break;
            case R.id.market_rank_m:
                setArrowChoice(drawableM, mRankNumMDown, 1);
                break;
            case R.id.market_rank_r:
                setArrowChoice(drawableR, mRankNumRDown, 2);
                break;
        }
    }

    private void setArrowChoice(Drawable draw, boolean mRankNum, int type) {
        mRankNum = mRankNum ? false : true;
        if (mRankNum)
            if (type == 0)
                draw = getResources().getDrawable(R.mipmap.down_b_arrow);
            else
                draw = getResources().getDrawable(R.mipmap.down_g_arrow);
        else
            draw = getResources().getDrawable(R.mipmap.up_b_arrow);

        draw.setBounds(0, 0, draw.getMinimumWidth(), draw.getMinimumHeight());


        switch (type) {
            case 0:
                mRankNumLDown = mRankNum;
                mMarketRankL.setCompoundDrawables(null, null, draw, null);
                break;
            case 1:
                mRankNumMDown = mRankNum;
                mMarketRankM.setCompoundDrawables(null, null, draw, null);
                break;
            case 2:
                mRankNumRDown = mRankNum;
                mMarketRankR.setCompoundDrawables(null, null, draw, null);
                break;
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View v, int position) {
        LogUtil.util(TAG, "返回数据是:" + v.getId() + "======" + position);
        switch (v.getId()) {
            case R.id.item_exchange_ly:
                Toast.makeText(getActivity(), "点击前段" + mList.get(position), Toast.LENGTH_SHORT).show();
                break;
            //case R.id.item_exchange_icon:
            // Toast.makeText(getActivity(), "点击后段" + mList.get(position), Toast.LENGTH_SHORT).show();
            // break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(mActivity, ThanExchangeActivity.class);

        Bundle bundle = new Bundle();
        bundle = setBundle(bundle, position);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    protected abstract Bundle setBundle(Bundle bundle, int position);
}
